package ysq.xyg.recovery.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.base.util.BaseUtil;
import ysq.xyg.recovery.dao.RecoveryWasteMemberDao;
import ysq.xyg.recovery.model.po.RecoveryWasteMember;
import ysq.xyg.recovery.model.vo.RecoveryWasteMemberVo;
import ysq.xyg.recovery.service.RecoveryWasteMemberService;
import ysq.xyg.sys.model.po.SysDictContent;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.web.model.po.WebMember;
import ysq.xyg.web.service.WebMemberService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 回收金模块-收废员service实现
 *
 * @author congcong tao
 * @date 2019-8-2 14:25:13
 */
@Service("recoveryWasteMemberService")
public class RecoveryWasteMemberServiceImpl extends BaseServiceImpl<RecoveryWasteMember> implements RecoveryWasteMemberService {

    @Autowired
    private WebMemberService webMemberService;
    @Resource
    private RecoveryWasteMemberDao recoveryWasteMemberDao;

    private RecoveryWasteMember recoveryWasteMember;

    @Autowired
    public void setBaseDao(RecoveryWasteMemberDao recoveryWasteMemberDao) {
        this.baseDao = recoveryWasteMemberDao;
    }

    /**
     * 分页查询
     *
     * @param recoveryWasteMemberVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-8-2 14:25:13
     */
    @Override
    public ReturnEntity page(RecoveryWasteMemberVo recoveryWasteMemberVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<RecoveryWasteMember> list = acceptList(recoveryWasteMemberVo);
        PageInfo<RecoveryWasteMember> page=new PageInfo<RecoveryWasteMember>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    @Override
    public ReturnEntity pageLeft(RecoveryWasteMemberVo recoveryWasteMemberVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        if (StringUtil.isNotEmpty(recoveryWasteMemberVo.getMemberNickName())) {
            recoveryWasteMemberVo.setMemberNickName(BaseUtil.encodeBase64String(recoveryWasteMemberVo.getMemberNickName()));
        }
        List<RecoveryWasteMember> list = recoveryWasteMemberDao.selectEntityList(recoveryWasteMemberVo);
        if (list != null && list.size() > 0) {
            for (RecoveryWasteMember recoveryWasteMember : list) {
                if (StringUtils.isNotEmpty(recoveryWasteMember.getMemberNickName())) {
                    recoveryWasteMember.setMemberNickName(BaseUtil.decodeBase64(recoveryWasteMember.getMemberNickName()));
                }
            }
        }
        PageInfo<RecoveryWasteMember> page=new PageInfo<RecoveryWasteMember>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param recoveryWasteMemberVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-8-2 14:25:13
     */
    @Override
    public ReturnEntity addOrUpdate(RecoveryWasteMemberVo recoveryWasteMemberVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(recoveryWasteMemberVo.getId())){
            recoveryWasteMember=new RecoveryWasteMember(currentUser);
            isUpate=false;
        }else{
            recoveryWasteMember=selectByPrimaryKey(recoveryWasteMemberVo.getId());
            if(ObjectUtil.isNull(recoveryWasteMember)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            recoveryWasteMember.setUpdateTime(new Date());
            recoveryWasteMember.setUpdateUser(currentUser.getId());
        }
        recoveryWasteMember.setMemberId(recoveryWasteMemberVo.getMemberId());
        recoveryWasteMember.setName(recoveryWasteMemberVo.getName());
        recoveryWasteMember.setPhone(recoveryWasteMemberVo.getPhone());
        recoveryWasteMember.setAddress(recoveryWasteMemberVo.getAddress());
        recoveryWasteMember.setWorkStatus(recoveryWasteMemberVo.getWorkStatus());
        recoveryWasteMember.setIdCard(recoveryWasteMemberVo.getIdCard());
        recoveryWasteMember.setCardImage(recoveryWasteMemberVo.getCardImage());
        recoveryWasteMember.setContent(recoveryWasteMemberVo.getContent());
        recoveryWasteMember.setStatus(recoveryWasteMemberVo.getStatus());
        recoveryWasteMember.setManageContent(recoveryWasteMemberVo.getManageContent());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(recoveryWasteMember);
        }else{
            num=insert(recoveryWasteMember);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }

    @Override
    public ReturnEntity addOrUpdate(RecoveryWasteMemberVo recoveryWasteMemberVo) {
        boolean isUpate = true;
        if(StrUtil.isEmpty(recoveryWasteMemberVo.getId())){
            recoveryWasteMember=new RecoveryWasteMember();
            recoveryWasteMember.setId(BaseUtil.uId());
            recoveryWasteMember.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_NORMAL);
            recoveryWasteMember.setCreateTime(DateUtil.date());
            recoveryWasteMember.setUpdateTime(DateUtil.date());
            isUpate=false;
        }else{
            recoveryWasteMember=selectByPrimaryKey(recoveryWasteMemberVo.getId());
            if(ObjectUtil.isNull(recoveryWasteMember)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            recoveryWasteMember.setUpdateTime(new Date());
        }
        recoveryWasteMember.setMemberId(recoveryWasteMemberVo.getMemberId());
        recoveryWasteMember.setName(recoveryWasteMemberVo.getName());
        recoveryWasteMember.setPhone(recoveryWasteMemberVo.getPhone());
        recoveryWasteMember.setAddress(recoveryWasteMemberVo.getAddress());
        recoveryWasteMember.setWorkStatus(recoveryWasteMemberVo.getWorkStatus());
        recoveryWasteMember.setIdCard(recoveryWasteMemberVo.getIdCard());
        recoveryWasteMember.setCardImage(recoveryWasteMemberVo.getCardImage());
        recoveryWasteMember.setContent(recoveryWasteMemberVo.getContent());
        recoveryWasteMember.setStatus(recoveryWasteMemberVo.getStatus());
        recoveryWasteMember.setManageContent(recoveryWasteMemberVo.getManageContent());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(recoveryWasteMember);
        }else{
            num=insert(recoveryWasteMember);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-8-2 14:25:13
     */
    @Override
    public ReturnEntity delete(String id, SysUser currentUser) {
        RecoveryWasteMember recoveryWasteMember=selectByPrimaryKey(id);
        if(recoveryWasteMember!=null){
            recoveryWasteMember.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            recoveryWasteMember.setUpdateTime(new Date());
            recoveryWasteMember.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(recoveryWasteMember);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param recoveryWasteMemberVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author congcong tao
     * @date 2019-8-2 14:25:13
     */
    @Override
    public List<RecoveryWasteMember> acceptList(RecoveryWasteMemberVo recoveryWasteMemberVo){
        Example example = new Example(RecoveryWasteMember.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(recoveryWasteMemberVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",recoveryWasteMemberVo.getRemoveTag());
        }
        if(StringUtil.isNotEmpty(recoveryWasteMemberVo.getMemberId())){
            criteria.andEqualTo("memberId",recoveryWasteMemberVo.getMemberId());
        }
        if(StringUtil.isNotEmpty(recoveryWasteMemberVo.getStatus())){
            if (StringUtils.equals(recoveryWasteMemberVo.getStatus(), BaseConstant.RECOVERY_WASTE_MEMBER_STATUS_FOUR)) {
                criteria.andNotEqualTo("status", BaseConstant.RECOVERY_WASTE_MEMBER_STATUS_THREE);
            } else {
                criteria.andEqualTo("status",recoveryWasteMemberVo.getStatus());
            }
        }
        example.setOrderByClause(" update_time desc");
        return recoveryWasteMemberDao.selectByExample(example);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public ReturnEntity updateStatus(String ids, String status, String manageContent) {
        if (StringUtils.isEmpty(ids)) {
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        String[] idArr = ids.split(",");
        for (String id : idArr) {
            RecoveryWasteMember recoveryWasteMember = this.selectByPrimaryKey(id);
            recoveryWasteMember.setStatus(status);
            recoveryWasteMember.setManageContent(manageContent);
            int info = this.updateByPrimaryKeySelective(recoveryWasteMember);
            if (info <= 0) {
                return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
            }
            // 如果同意，则针对的修改webMember的值
            if (StringUtils.equals(status, BaseConstant.RECOVERY_WASTE_MEMBER_STATUS_TWO)) {
                WebMember webMember = webMemberService.selectByPrimaryKey(recoveryWasteMember.getMemberId());
                webMember.setRecoveryPermission(BaseConstant.RECOVERY_PERMISSION_YES);
                webMemberService.updateByPrimaryKey(webMember);
            }
        }
        return new ReturnEntity();
    }

    @Override
    public ReturnEntity acceptSelect(String code) {
        RecoveryWasteMemberVo wasteMemberVo = new RecoveryWasteMemberVo();
        wasteMemberVo.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_NORMAL);
        wasteMemberVo.setStatus(BaseConstant.RECOVERY_WASTE_MEMBER_STATUS_TWO);
        List<RecoveryWasteMember> list = acceptList(wasteMemberVo);
        List<SysDictContent> sysDictContentList=new ArrayList<SysDictContent>();
        for(RecoveryWasteMember entity : list){
            SysDictContent sysDictContent=new SysDictContent();
            sysDictContent.setId(entity.getId());
            sysDictContent.setName(entity.getName() + "(" + entity.getAddress() + ")");
            sysDictContent.setDictType(code);
            sysDictContent.setValue(entity.getId());
            sysDictContentList.add(sysDictContent);
        }
        return new ReturnEntity(sysDictContentList);
    }
}
